package com.example.mapper;

import com.example.pojo.Article;
import com.example.pojo.Favorite;
import com.example.pojo.Like;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface InteractMapper {
    //是否点赞过


//    添加点赞
    @Insert("insert into `like`(article_id,user_id,update_time,status) values (#{articleId},#{userId},#{updateTime},#{status})")
    void addlike(Like like);

    @Insert("insert into `favorite`(article_id,user_id,create_time) values (#{articleId},#{userId},#{createTime})")
    void collect(Favorite favorite);

    @Update("update `like` set status = 0,update_time = #{updateTime} where article_id = #{id} and user_id = #{userid}")
    void unlike(Integer id, Integer userid, LocalDateTime updateTime);

    @Delete("delete from favorite where article_id = #{id} and user_id = #{userid}")
    void uncollect(Integer id, Integer userid);

    @Select("select article.id,article.title,article.cover_img,article.create_time from `article` where article.id in (select article_id from `favorite` where user_id = #{userid})")
    List<Article> mycollect(Integer userid);

    @Select("select article.id,article.title,article.cover_img,article.create_time from `article` where article.id in (select article_id from `like` where user_id = #{userid} and status = 1)")
    List<Article> mylike(Integer userid);

    @Select("select * from `like` where article_id = #{id} and user_id = #{userid}")
    Like ifliked(Integer id, Integer userid);

    //修改点赞状态
    @Update("update `like` set status = #{status},update_time = #{updateTime} where article_id = #{articleId} and user_id = #{userId}")
    void updateStatus(Like like);
}
